<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/album/SavePhoto.class.php
 *
 * 保存用户上传的照片
 *
 * PHP Version 5
 *
 * @package:   class.album
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once CLASS_PATH.'main/BaseAction.class.php';
include_once CLASS_PATH.'user/UserUtil.class.php';
include_once CLASS_PATH.'image/Image.class.php';

//包含相册的工具表
include_once CLASS_PATH.'album/AlbumUtil.class.php';



class SavePhoto extends BaseAction {
   /**
    * 数据库的连接
    */
   private $db;


   /**
    * 构造函数
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function __construct() {
      $this->db = $this->getDB();
   }


   /**
    * 保存用户上传的照片
    * @param:  NULL
    * @return: NULL
    * @access: public
    */
   public function run() {

      $user_name = $_SESSION['user']['name'];
      $user_id = UserUtil::getUserId($this->db, $user_name);

      $dir_id = $this->getParameterFromPOST('dir');
      
      if ( !$dir_id ) {
         $this->AlertAndBack('没有选择相片的目录');
         return;
      }

      //判断目录是否存在
      if ( !AlbumUtil::isDirExists($this->db, $dir_id) ) {
         $this->AlertAndBack('目录不存在.');
         return;
      }

      //判断目录是否是用户拥有
      if ( !AlbumUtil::dirBelongToUser($this->db, $dir_id, $user_id) ) {
         $this->AlertAndBack('您不能上传相片到其他用户目录.');
         return;
      }


      set_time_limit(0);

      //收集变量
      $name       = $this->getParameterFromPOST('name');       //照片的名称
      $descrite   = $this->getParameterFromPOST('description');   //照片的描述

      //进行变量的判断
      if ( !$name ) {
         echo "<script language=javascript>\n";
         echo "alert('相片的名称不能为空。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }

      if ( strlen($name) > 50 ) {
         echo "<script language=javascript>\n";
         echo "alert('相片的名称最多只能为25个汉字。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }

      //查看是否有重名的文件名
      $sql  = "select count(name) as num from album where name='$name' ".
         " and user_id='$user_id' and dir_id='$dir_id' ";
      $res  = $this->db->Execute($sql);
      $rows = $res->FetchRow();

      $num  = $rows['num'];

      if ( $num ) {
         echo "<script language=javascript>\n";
         echo "alert('名称为：".$name." 的相片已经存在。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }


      if ( strlen($descrite) > 250 ) {
         echo "<script language=javascript>\n";
         echo "alert('相片的描述最多只能为125个汉字。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }

      //检查图片的类型
      $type = $this->getParameterFromPOST('type');

      if ( !$type || ! AlbumUtil::isExists($this->db, $type) ) {
         $type = 1;
      }


      //这些条件都满足了。
      //开始对用户传上来的文件进行判断

      //放置相片的路径
      $photo_put_dir = ROOT_PATH.'upload/album/'.$user_id.'/';

      //判断照片文件的类型是否符合要求
      if ( $_FILES['photo_file']['type'] != 'image/gif' && $_FILES['photo_file']['type'] != 'image/jpeg' && $_FILES['photo_file']['type'] != 'image/jpg' && $_FILES['photo_file']['type'] != 'image/pjpeg' && $_FILES['photo_file']['type'] != 'image/png' ) {
         echo "<script language=javascript>\n";
         echo "alert('上传的照片的文件格式只能是jpeg, gif, png三种格式。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }

      //判断上传的文件大小是否合乎要求
      if ( $_FILES['photo_file']['size'] > 1048576 ) {
         echo "<script language=javascript>\n";
         echo "alert('上传的照片的文件的大小不能超过1M。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }
      

      //取得文件的大小
      list($image_width, $image_height, $image_type, $image_attr ) = getimagesize($_FILES['photo_file']['tmp_name']);

      $image_fact_type = '';

      //判断文件的类型
      switch ( $image_type ) {
         case 1:
            $image_left_type = '.gif';
            $image_fact_type = 'gif';
            break;
         case 2:
            $image_left_type = '.jpg';
            $image_fact_type = 'jpg';

            break;
         case 3:
            $image_left_type = '.png';
            $image_fact_type = 'png';

            break;
      }


      //写插入语句
      $sql  = "insert into album (user_id, name, size, type, dir_id, description, image_type ) values ('";
      $sql .= $user_id."', '".$name."', '".$image_width."*".$image_height."', '";
      $sql .= $type."', '".$dir_id."', '".$descrite."','".$image_fact_type."') ";

      $this->db->Execute($sql);

      if ( $this->db->ErrorNo() ) {
         die('在上传照片时插入数据库错误。<br>'.$this->db->ErrorMsg());
      }

      //得到刚刚插入的数据库记录的id
      $image_id = $this->db->Insert_id();

      //图像的名字
      $image_name = $photo_put_dir.$image_id.$image_left_type;

      //将图像文件移到相应的目录中去

      if ( !move_uploaded_file($_FILES['photo_file']['tmp_name'], $image_name ) ) {
         $sql = "delete from album where id='$image_id' ";
         $this->db->Execute($sql);
         echo "<script language=javascript>\n";
         echo "alert('上传的照片失败。');\n";
         echo "history.back();\n";
         echo "</script>\n";
         return;
      }

      //更新数据库，使其记录图片文件的名字
      $file_name = $image_id.'-117-83'.$image_left_type;
      $file_name1 = $image_id.'-460-390'.$image_left_type;

      $file_name = $photo_put_dir.$file_name;
      $file_name1 = $photo_put_dir.$file_name1;

      //如果上传文件成功
      //则开始制作文件的缩略图
      //生成缩略图
      //现确定高度和宽度。

      $my_image = new Image();
      //设置原文件
      $my_image->setSrcFile($image_name);
      //设置目的文件
      $my_image->setDstFile($file_name);
      //设置宽度
      $my_image->setResizeWidth(117);
      $my_image->setResizeHeight(83);
      //生成一个缩略图
      $my_image->ResizeImage();

      //再生成一个
      $my_image->setDstFile($file_name1);
      $my_image->setResizeWidth(460);
      $my_image->setResizeHeight(390);
      //在生成一个缩略图
      $my_image->ResizeImage();

      set_time_limit(60);

      echo "<script language=javascript>\n";
      echo " if ( confirm('上传照片成功，继续上传吗？') ) { \n";
      echo "  document.location.href='".$this->my_url."?module=album&action=upload&dir=".$dir_id."';\n";
      echo " } else { \n";
      echo "  document.location.href='".$this->my_url."?module=album&action=takephoto&dir=".$dir_id."';\n";
      echo " } \n";
      echo "</script>\n";
      return;
   }
}

?>
